package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.PdxmfsJs;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class PdxmfsJsForm extends BasePage implements Serializable {
    private GenericManager<PdxmfsJs, Long> pdxmfsJsManager;
    private PdxmfsJs pdxmfsJs = new PdxmfsJs();
    private Long id;

    public void setPdxmfsJsManager(GenericManager<PdxmfsJs, Long> pdxmfsJsManager) {
        this.pdxmfsJsManager = pdxmfsJsManager;
    }

    public PdxmfsJs getPdxmfsJs() {
        return pdxmfsJs;
    }

    public void setPdxmfsJs(PdxmfsJs pdxmfsJs) {
        this.pdxmfsJs = pdxmfsJs;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        pdxmfsJsManager.remove(pdxmfsJs.getId());
        addMessage("pdxmfsJs.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            pdxmfsJs = pdxmfsJsManager.get(id);
        } else {
            pdxmfsJs = new PdxmfsJs();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (pdxmfsJs.getId() == null || pdxmfsJs.getId() == 0);
        
        pdxmfsJs.setUsername(getRequest().getRemoteUser());
        pdxmfsJs.setOptdate(new Date());
        pdxmfsJs.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        pdxmfsJsManager.save(pdxmfsJs);

        String key = (isNew) ? "pdxmfsJs.added" : "pdxmfsJs.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	 this.id=this.pdxmfsJs.getId();
             return edit();
        }
    }
} 